package com.danger.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Presenter.ObjectInfoDetailPresenter;
import com.danger.Presenter.ObjectInfoDetailPresenterImpl;
import com.danger.Util.ImageLoaderUtil;
import com.danger.View.ObjectDetailView;

public class ObjectInfoDetailActivity extends AppCompatActivity implements ObjectDetailView {

    private CollapsingToolbarLayout toolbar_layout;
    private TextView objectinfo_detail_title_textView;
    private TextView objectinfo_detai_title;
    private TextView objectinfo_detail_category_textView;
    private TextView objectinfo_detail_category;
    private TextView objectinfo_detail_describe_textView;
    private TextView objectinfo_detail_describe;
    private TextView objectinfo_detail_address_textView;
    private TextView objectinfo_detail_address;
    private TextView objectinfo_detail_time_textView;
    private TextView objectinfo_detail_time;
    private TextView objectinfo_detail_name_textView;
    private TextView objectinfo_detail_phone_textView;
    private TextView objectinfo_detail_qq_textView;
    private Intent intent;
    private Toolbar toolbar;
    private Handler handler = new Handler();
    private String id;
    private ObjectInfoDetailPresenter presenter;
    private ObjectInfo objectInfo;
    private FloatingActionButton object_info_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_object_info_detail);

        initView();
        initEvent();
    }

    private void initView() {
        toolbar_layout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        object_info_phone = (FloatingActionButton) findViewById(R.id.object_info_phone);
        objectinfo_detail_title_textView = (TextView) findViewById(R.id.objectinfo_detail_title_textView);
        objectinfo_detai_title = (TextView) findViewById(R.id.objectinfo_detai_title);
        objectinfo_detail_category_textView = (TextView) findViewById(R.id.objectinfo_detail_category_textView);
        objectinfo_detail_category = (TextView) findViewById(R.id.objectinfo_detail_category);
        objectinfo_detail_describe_textView = (TextView) findViewById(R.id.objectinfo_detail_describe_textView);
        objectinfo_detail_describe = (TextView) findViewById(R.id.objectinfo_detail_describe);
        objectinfo_detail_address_textView = (TextView) findViewById(R.id.objectinfo_detail_address_textView);
        objectinfo_detail_address = (TextView) findViewById(R.id.objectinfo_detail_address);
        objectinfo_detail_time_textView = (TextView) findViewById(R.id.objectinfo_detail_time_textView);
        objectinfo_detail_time = (TextView) findViewById(R.id.objectinfo_detail_time);
        objectinfo_detail_name_textView = (TextView) findViewById(R.id.objectinfo_detail_name_textView);
        objectinfo_detail_phone_textView = (TextView) findViewById(R.id.objectinfo_detail_phone_textView);
        objectinfo_detail_qq_textView = (TextView) findViewById(R.id.objectinfo_detail_qq_textView);
        presenter = new ObjectInfoDetailPresenterImpl(this, this);
        intent = getIntent();
        toolbar = (Toolbar) findViewById(R.id.objectinfo_detail_toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        if (intent != null) {
            objectInfo = (ObjectInfo) intent.getSerializableExtra("objectInfo");
            String url = objectInfo.getPhoto().toString();
            if (url == "" || url == null) {
                toolbar_layout.setBackgroundResource(R.drawable.black);
            } else {
                new ImageLoaderUtil(toolbar_layout, url, this).loadLayoutBackGround();
            }
            if (objectInfo.isLost()) {
                toolbar.setTitle("失物详情页");
                objectinfo_detai_title.setText("失物名称：  ");
                objectinfo_detail_category.setText("失物类别：  ");
                objectinfo_detail_describe.setText("失物描述：  ");
                objectinfo_detail_address.setText("丢失地点：  ");
                objectinfo_detail_time.setText("丢失时间：  ");
            } else {
                toolbar.setTitle("招领详情页");
                objectinfo_detai_title.setText("拾物名称：  ");
                objectinfo_detail_category.setText("拾物类别：  ");
                objectinfo_detail_describe.setText("拾物描述：  ");
                objectinfo_detail_address.setText("拾物地点：  ");
                objectinfo_detail_time.setText("拾物时间：  ");
            }
            id = objectInfo.getObjectId();
            objectinfo_detail_title_textView.setText(objectInfo.getTitle());
            objectinfo_detail_category_textView.setText(objectInfo.getCategory());
            objectinfo_detail_describe_textView.setText(objectInfo.getDescribe());
            objectinfo_detail_address_textView.setText(objectInfo.getAddress());
            objectinfo_detail_time_textView.setText(objectInfo.getDate().getDate().toString());
            objectinfo_detail_name_textView.setText(objectInfo.getName());
            objectinfo_detail_phone_textView.setText(objectInfo.getPhone());
            objectinfo_detail_qq_textView.setText(objectInfo.getQq());
        }
        setSupportActionBar(toolbar);
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.detail_share:
                        presenter.share();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        object_info_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ObjectInfoDetailActivity.this);
                builder.setTitle("提示");
                builder.setMessage("联系" + objectinfo_detail_name_textView.getText().toString() + "?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ObjectInfoDetailActivity.this.toCall();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }

    @Override
    public void showSuccess(String s, ObjectInfo object) {
        if (objectInfo != null) {
            objectInfo = object;
        }
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void back() {
        finish();
    }

    @Override
    public String getShareContent() {
        String title = objectinfo_detai_title.getText().toString().trim() + objectInfo.getTitle().toString() + "\n";
        String category = objectinfo_detail_category.getText().toString().trim() + objectInfo.getCategory().toString() + "\n";
        String describe = objectinfo_detail_describe.getText().toString().trim() + objectInfo.getDescribe().toString() + "\n";
        String address = objectinfo_detail_address.getText().toString().trim() + objectInfo.getAddress().toString() + "\n";
        String time = objectinfo_detail_time.getText().toString().trim() + objectInfo.getDate().getDate().toString() + "\n";
        String name = "发布人:" + objectInfo.getName().toString() + "\n";
        String phone = "联系电话：" + objectInfo.getPhone().toString() + "\n";
        String qq = "qq:" + objectInfo.getQq().toString() + "\n";
        String content = title + category + describe + address + time + name + phone + qq;
        return content;
    }

    @Override
    public String getShareImageUrl() {
        return objectInfo.getPhoto();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_object_info_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void toCall() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + objectinfo_detail_phone_textView.getText().toString()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}